package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.mapper.TokenMapper;
import com.itheima.mapper.UsersMapper;
import com.itheima.pojo.BsUser;
import com.itheima.service.UsersService;
import com.itheima.util.Token;
import com.itheima.util.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private TokenMapper tokenMapper;

    @Override
    public List<Users> findById(String s) {
        LambdaQueryWrapper<Token> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Token::getToken,s);
        List<Token> tokens = tokenMapper.selectList(wrapper);
        //创建返回值集合对象
        List<Users> bsUsers = new ArrayList<>();
        for (Token token : tokens) {
            //获取到登录者的id
            Integer bsUserId = token.getBsUserId();
            //根据登录者id查询数据
            BsUser bsUser = usersMapper.selectById(bsUserId);
            //创建返回值对象
            Users users = new Users();
            //封装对象
            users.setEmail(bsUser.getEmail());
            users.setId(bsUser.getId());
            users.setPhone(bsUser.getPhone());
            users.setUsername(bsUser.getUsername());
            bsUsers.add(users);
        }
        return bsUsers;
    }
}
